using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.IO;
using My.IdlConverter.Core.Util;
using My.IdlConverter.Sdkmachine.Config;

namespace My.IdlConverter.Sdkmachine
{
    public class WorkshopBase : IWorkshop
    {
        private StringCollection subDirs = new StringCollection();

        private static readonly string VosIncludePrefix = "VOS\\Interface\\";
        private static readonly string PubIncludePrefix = "Public\\Interface\\";

        public WorkshopBase()
        {
        }

        public virtual void ProductDomInclude(string relativeDomPath, string IdlConverterPath, string targetPath)
        {
            SdkmachineConfig config = new SdkmachineConfig();

            IdlConverterPath = Path.Combine(IdlConverterPath, relativeDomPath);
            targetPath = Path.Combine(targetPath, "build\\includes\\dom");
            IEnumerable<string> allFiles = config.GetDomFiles(IdlConverterPath);
            foreach(string nextFile in allFiles)
            {
                string relativePathName = nextFile.Replace(IdlConverterPath,"").Remove(0,1);
                PathHelper.CopyFile(Path.Combine(IdlConverterPath, nextFile), Path.Combine(targetPath, relativePathName), true);
            }
        }

        public virtual void ProductPubicInclude(StringCollection relativeFilePaths, string IdlConverterPath, string targetPath)
        {
            string sdkIncludePath = Path.Combine(targetPath, "build\\includes");

            foreach(string nextFile in relativeFilePaths)
            {
                string srcFilePath = Path.Combine(IdlConverterPath, nextFile);
                string dstFilePath = Path.Combine(sdkIncludePath, GetPureFileName(nextFile));
                PathHelper.CopyFile(srcFilePath, dstFilePath, true);
            }
        }

        public virtual void ProductLib(IDictionary<string, StringCollection> relativeLibFilePaths, string IdlConverterPath, string targetPath)
        {
            CopyLibFile(IdlConverterPath, targetPath, relativeLibFilePaths, "Debug");
            CopyLibFile(IdlConverterPath, targetPath, relativeLibFilePaths, "Release");
        }

        public virtual void ProductBin(string targetPath)
        {
            PathHelper.RecursiveToCreateDirectory(Path.Combine(targetPath, "build\\bin"));
        }
        public virtual void ProductObj(string targetPath)
        {
            PathHelper.RecursiveToCreateDirectory(Path.Combine(targetPath, "build\\obj"));
        }
        public virtual void ProductSrc(string targetPath)
        {
            PathHelper.RecursiveToCreateDirectory(Path.Combine(targetPath, "build\\src\\Interface"));
            PathHelper.RecursiveToCreateDirectory(Path.Combine(targetPath, "build\\src\\Modules"));
        }
        public virtual void ProductDocs(StringCollection absoluteDocsPaths, string targetPath)
        {
            PathHelper.RecursiveToCreateDirectory(Path.Combine(targetPath, "docs\\reference"));
            PathHelper.RecursiveToCreateDirectory(Path.Combine(targetPath, "docs\\programming-guide"));
        }
        public virtual void ProductTools(StringCollection absoluteToolsPaths, string targetPath)
        {
            PathHelper.RecursiveToCreateDirectory(Path.Combine(targetPath, "tools"));
        }
        public virtual void ProductSamples(StringCollection absoluteSamplesPaths,string targetPath)
        {
            PathHelper.RecursiveToCreateDirectory(Path.Combine(targetPath, "build\\samples"));
        }

        private string GetPureFileName(string pathName)
        {
           int index = pathName.LastIndexOf(PubIncludePrefix);
            if (index == -1)
            {
                index = pathName.LastIndexOf(VosIncludePrefix);
                System.Diagnostics.Debug.Assert(index != -1);
                return pathName.Replace(VosIncludePrefix, "").Trim();
            }
            else 
                return pathName.Replace(PubIncludePrefix, "").Trim();
        }

        private void CopyLibFile(string IdlConverterPath, string targetPath, IDictionary<string, StringCollection> relativeLibFilePaths,string version)
        {
            StringCollection __relativeLibPaths = relativeLibFilePaths[version];

            foreach (string nextLibFile in __relativeLibPaths)
            {
                string srcFilePath = Path.Combine(IdlConverterPath, nextLibFile);
                string fileName = nextLibFile.Substring(nextLibFile.LastIndexOf('\\') + 1);
                PathHelper.CopyFile(srcFilePath, Path.Combine(Path.Combine(targetPath, String.Format("build\\lib\\{0}",version)), fileName), true);
            }
        }
    }
}
